package m.jclecai.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.Response.ResponseTaskBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.dialog.ShareDialog;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.BaseBody;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

public class TaskActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.rl_refresh_layout)
    AutoSwipeRefreshLayout mRlRefrshLayout;

    @BindView(R.id.rv_sample_task)
    RecyclerView mRvSampleTask;
    @BindView(R.id.rv_recommand_task)
    RecyclerView mRvRecommandTask;


    private TaskAdapter mSampleTaskAdapter;
    private TaskAdapter mRecommandTaskAdapter;
    private UserBean mUserBean;
    private ShareDialog mShareDialog;


    @Override
    protected int getContentView() {
        return R.layout.activity_task;
    }


    @Override
    protected void initView() {

        setTitle("娱乐厅");
        leftOptionText("");
        mSampleTaskAdapter = new TaskAdapter(null);
        mRvSampleTask.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvSampleTask.setAdapter(mSampleTaskAdapter);

        mRecommandTaskAdapter = new TaskAdapter(null);
        mRvRecommandTask.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvRecommandTask.setAdapter(mRecommandTaskAdapter);


    }


    @Override
    protected void initData() {
        initUser();

        mRlRefrshLayout.setOnRefreshListener(this);
        mRlRefrshLayout.setRefreshing(true);
        requestTaskData();
    }


    private void requestTaskData() {

        String userName = PreferenceUtil.getUserName(this);
        String password = PreferenceUtil.getPassword(this);
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);
        pair.put("password", password);
        HttpManager.postAsyn(Constant_Url.TASK_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                if (mRlRefrshLayout != null)
                    mRlRefrshLayout.setRefreshing(false);
                DisPlay("请检查网络！！！");
            }

            @Override
            public void onResponse(String response) {
                if (mRlRefrshLayout != null)
                    mRlRefrshLayout.setRefreshing(false);

                try {
                    ResponseTaskBean responseSignList = FastOk.get(response, ResponseTaskBean.class);
                    if(responseSignList!=null){
                        if ("0".equals(responseSignList.getStatus())) {
                            mSampleTaskAdapter.refresh(responseSignList.getRecords().get(0));
                            mRecommandTaskAdapter.refresh(responseSignList.getRecords().get(1));
                        } else {
                            DisPlay(responseSignList.getMessage());
                        }
                    }else{
                        DisPlay("网络异常！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络异常！");
                }

            }
        }, pair);
    }

    private void initUser() {
        mUserBean = PreferenceUtil.readUser(this);
    }

    @Override
    public void onRefresh() {
        requestTaskData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class TaskAdapter extends BaseRecyclerViewAdapter<ResponseTaskBean.TaskBean> {

        @BindView(R.id.tv_task_money)
        TextView mTvTaskMoney;
        @BindView(R.id.tv_task_content)
        TextView mTvTaskContent;
        @BindView(R.id.tv_complete_task)
        TextView mTvCompleteTask;

        public TaskAdapter(List<ResponseTaskBean.TaskBean> DList) {
            super(DList);

        }

        @Override
        public int getItemView() {
            return R.layout.item_task;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final ResponseTaskBean.TaskBean bean, int position) {


            holder.getTextView(R.id.tv_task_content).setText(bean.getTitle());
            holder.getTextView(R.id.tv_task_money).setText("+" + bean.getMoney());

            final TextView rightButton = holder.getTextView(R.id.tv_complete_task);
            rightButton.setEnabled(true);

            if ("0".equals(bean.getState())) {
                //没有完成
                rightButton.setText("去完成");
                rightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //判断一下具体哪一个
                        if (mUserBean != null) {
                            //这里表示登陆了我们去访问网络；
                            goStartTask(bean.getTitle());
                        } else {
                            DisPlay("请先登录！！！");
                            checkLogin(new Callback() {
                                @Override
                                public void callbackSuccess() {
                                    //重新请求网络
                                    requestTaskData();
                                }
                            });
                        }
                    }
                });
            } else if ("1".equals(bean.getState())) {
                //
                rightButton.setText("领取");
                rightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestReward(bean.getId(), rightButton);
                    }
                });
            } else if ("2".equals(bean.getState())) {
                rightButton.setText("已完成");
                //设置按钮变灰色的
                rightButton.setEnabled(false);
            }

        }
    }

    private void goStartTask(String title) {
        //这里的话，我们都是需要对其进行一个判断是否登陆，如果没有登陆我们是需要跳转到登陆页面；
        if (title.contains("手机号")) {
            startActivity(new Intent(this, BindPhoneActivity.class));
        } else if (title.contains("实名认证")) {
            startActivity(new Intent(this, BindIDCardActivity.class));
        } else if (title.contains("银行卡")) {
            startActivity(new Intent(this, BankCardListActivity.class));
        } else if (title.contains("充值")) {
//            startActivity(new Intent(this, RechargeActivity.class));
            startActivity(new Intent(this, RechargePayActivity.class));

        } else if (title.contains("分享")) {
            //OpenShareAnimation();
            if (mShareDialog == null) {
                 mShareDialog = new ShareDialog(this);
            }
            mShareDialog.show();
        } else {
            finish();
        }
    }

    private void requestReward(String taskId, final TextView button) {
        if (mUserBean == null) {
            return;
        }

        String userName = PreferenceUtil.getUserName(this);
        String password = PreferenceUtil.getPassword(this);
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);
        pair.put("password", password);
        pair.put("id", taskId);
        HttpManager.postAsyn(Constant_Url.TASK_REWARD, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                //
                DisPlay("请检查网络！！");
            }

            @Override
            public void onResponse(String response) {
                System.out.println(response);
                try {
                    BaseBody responseSignList = FastOk.get(response, BaseBody.class);
                    if(responseSignList!=null){
                        if ("0".equals(responseSignList.getStatus())) {
                            //这里表示领取成功
                            button.setEnabled(false);
                            button.setText("已完成");
                            DisPlay("领取成功！！！");
                        } else {
                            DisPlay(responseSignList.getMessage());
                        }
                    }else{
                        DisPlay("请检查网络！！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("请检查网络！！");
                }
            }
        }, pair);
    }

}
